﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using LogQuery = Interop.MSUtil.LogQueryClassClass;
using IISW3CInputFormat = Interop.MSUtil.COMIISW3CInputContextClassClass;
using IISInputFormat = Interop.MSUtil.COMIISIISInputContextClassClass;
using EvtInputFormat = Interop.MSUtil.COMEventLogInputContextClassClass;
using DataGridOutput = Interop.MSUtil.COMDataGridOutputContextClassClass;
using LogRecordSet = Interop.MSUtil.ILogRecordset;

namespace LogParserEx
{
    public class Log
    {
        #region Attributes

        private string LogType = "IISW3C";
        private string QuerySQL = "";
        public bool atEnd = false;
        public Result myResult = new Result();
        public System.Windows.Forms.DataGridView dgview = null;

        private LogQuery oLogQuery = new LogQuery();
        private IISW3CInputFormat oIISW3CInputFormat = new IISW3CInputFormat();
        private EvtInputFormat oEvtInputFormat = new EvtInputFormat();
        private DataGridOutput oDataGridOutput = new DataGridOutput();
        private LogRecordSet oRecordSet; //oLogQuery.Execute(QuerySQL, oIISW3CInputFormat);

        #endregion

        #region 构造函数

        public Log()
        {
        }

        public Log(string type,string sql)
        {
            LogType = type;
            QuerySQL = sql;

            Query(QuerySQL);
        }

        #endregion

        private void Query(string sql)
        {
            QuerySQL=sql;
            oDataGridOutput.rowsToPrint = 100;
            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
            switch (LogType)
            {
                case "IISW3C":
                    oRecordSet =  oLogQuery.Execute(QuerySQL, oIISW3CInputFormat);
                    break;
                case "EVT":
                    oRecordSet = oLogQuery.Execute(QuerySQL, oEvtInputFormat);
                    break;
                default:
                    oRecordSet = oLogQuery.Execute(QuerySQL, oIISW3CInputFormat);
                    break;
            }
            
            myResult.sqlTime = watch.Elapsed;
            watch.Stop();

            if (oRecordSet.atEnd()) atEnd = true;
        }

        public DataTable getData()
        {
            if (oRecordSet.atEnd()) return null;

            System.Diagnostics.Stopwatch watch = System.Diagnostics.Stopwatch.StartNew();
            DataTable dt = new DataTable();
            for (int i = 0; i < oRecordSet.getColumnCount(); i++)
            {
                DataColumn dc = new DataColumn(oRecordSet.getColumnName(i));
                switch (oRecordSet.getColumnType(i))
                {
                    case 1: // int32
                        dc.DataType = typeof(int);
                        break;
                    case 2: // double
                        dc.DataType = typeof(double);
                        break;
                    case 4: // datetime
                        dc.DataType = typeof(string);
                        break;
                    default:
                        dc.DataType = typeof(string);
                        break;
                }
                dt.Columns.Add(dc);
            }
            int rowscount = 0;
            while (!oRecordSet.atEnd())
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i < oRecordSet.getColumnCount(); i++)
                {
                    if (!oRecordSet.getRecord().isNull(i))
                    {
                        dr[i] = oRecordSet.getRecord().getValue(i).ToString();
                    }
                }
                dt.Rows.Add(dr);
                rowscount++;
                oRecordSet.moveNext();
            }

            if (oRecordSet.atEnd()) atEnd = true;
            myResult.number = rowscount;
            
            myResult.displayTime = watch.Elapsed;
            watch.Stop();

            return dt;
        }
    }

    public struct Result
    {
        public int number;
        public TimeSpan sqlTime;
        public TimeSpan displayTime;
    }
}
